package com.k7k7.sserver.room;

import com.k7k7.sserver.client.ClientS;
import org.dom4j.Element;

import java.util.Collection;
import java.util.Vector;

public class Room {
	private static final int Max_User = 10;
	private final String roomID;
	private final Vector<ClientS> clients;

	public Room(String id) {
		this.roomID = id;
		this.clients = new Vector<ClientS>();
	}

	@SuppressWarnings("unchecked")
	public Vector<ClientS> getAllClients() {
		return clients;
	}

	public Vector<ClientS> getClientsExcept(Collection<ClientS> clis) {
		Vector<ClientS> t = (Vector<ClientS>) clients.clone();
		for (ClientS cli : clis) {
			t.remove(cli);
		}
		return t;
	}

	public boolean canEnter() {
		return clients.size() < Max_User;
	}

	public String getRoomID() {
		return roomID;
	}

	public void add(ClientS client) {
		clients.add(client);
	}

	public void remove(ClientS client) {
		this.clients.remove(client);
	}

	public void sendToAllClients(Element info) {
		for (ClientS cli : clients) {
			if (cli.isReady) {
				cli.send(info);
			}
		}
	}

	public ClientS getClientByID(String string) {
		for (ClientS c : clients) {
			if (string.equals(c.user.getId().toString())) {
				return c;
			}
		}
		return null;
	}
}
